package cn.finder.httpcommons.parser;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import cn.finder.httpcommons.domain.Message;
import cn.finder.httpcommons.response.ApiResponse;

/***
 * XML解析器
 * @author Administrator
 * 反射的字段 在T 中一定要存在 否则解析不成功
 * @param <T>
 */
public class XmlParser<T extends ApiResponse> implements IParser<T>{

	private String rootElement="xml";
	
	public void setRootElement(String rootElement){
		this.rootElement=rootElement;
	}
	
	public T parse(String body,Class<Object> type) {
		// TODO Auto-generated method stub
		XStream xs1 = new XStream(new DomDriver());

        xs1.alias(rootElement, type);
        xs1.ignoreUnknownElements();//暂时忽略掉一些新增的字段

       
        T reData = (T) xs1.fromXML(body);
        Message msg=new Message();
        reData.setMessage(msg);
        reData.setBody(body);
		return reData;
	}

	/*public <T extends ApiResponse> T parse(String body) {
		// TODO Auto-generated method stub
		return null;
	}*/
	

}
